<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\ApiRequest;
use App\Models\Notice;
use App\Models\NoticeUser;
use App\Models\Invite;
use App\Models\Question;
use DB;

class NoticeController extends Controller
{
    public function index(ApiRequest $request)
    {
        $rs['status']  = true;       
        $where = [];
        $where['to_user_id'] = $request->user_id;
        $list = DB::table('notice_users as nu')
                    ->select(['nu.is_read','n.title','n.describe','n.created_at','n.id'])
                    ->leftJoin('notices as n','n.id','=','nu.notice_id')
                    ->where($where)
                    ->paginate(10);

       
        $rs['data'] = $list;
        return response()->json($rs);   
    }

    public function show(ApiRequest $request)
    {
    	$rs['status'] = true;
    	$id 		= (int)$request->id;        
    	$rs['data'] = Notice::find($id);
        $where['notice_id']  = $id;
        $where['to_user_id'] = $request->user_id;
        $where['is_read']    = 0;
        $nu = NoticeUser::where($where)->first();
        if($nu) {
            $nu->is_read = 1;
            $nu->save();
        }
    	return response()->json($rs);

    }

    public function tipnum(ApiRequest $request)
    {
        $rs['status'] = true;
        $where['to_user_id'] = $request->user_id;
        $where['is_read'] = 0;
        $num1 = NoticeUser::where($where)->count();
        $num2 = Invite::where($where)->count();
        $num3 = Question::where($where)->count();
        // 系统公告 0
        $rs['data']['num'] = $num1+$num2+$num3;
        $rs['data']['notice_num']   = $num1;
        $rs['data']['invite_num']   = $num2;
        $rs['data']['question_num'] = $num3;
        return response()->json($rs);
    }



   
}
